package com.innovation.chengzhangriji.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.ui.SelectActivity;
import com.innovation.chengzhangriji.ui.model.UINote;

public class NoteAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements NoteViewHolder.OnClickNoteListener, AddViewHolder.OnClickAddListener {

    public interface OnClickNoteListener {
        void onClickNote(int position);

        void onLongClickNote(int position);
    }

    private OnClickNoteListener mOnClickNoteListener;

    private Context mContext;

    private UINote[] mNotes;

    public NoteAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view;
        RecyclerView.ViewHolder viewHolder;
        switch (viewType) {
            case 0:
                view = inflater.inflate(R.layout.item_note, parent, false);
                viewHolder = new NoteViewHolder(view);
                ((NoteViewHolder) viewHolder).setOnClickNoteListener(this);
                return viewHolder;
            case 1:
                view = inflater.inflate(R.layout.item_special_note, parent, false);
                viewHolder = new AddViewHolder(view);
                ((AddViewHolder) viewHolder).setOnClickAddListener(this);
                return viewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 0:
                onBindNoteViewHolder(holder, position);
                break;
            case 1:
                break;
        }
    }

    @Override
    public int getItemCount() {
        if (mNotes == null || mNotes.length == 0) {
            return 1;
        } else if (mNotes.length < 4) {
            return mNotes.length + 1;
        } else {
            return mNotes.length;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (getItemCount() == 1) {
            return 1;
        } else if (getItemCount() == 2) {
            if (position == 0) {
                return 0;
            } else if (position == 1) {
                return 1;
            }
        } else if (getItemCount() == 3) {
            if (position == 0) {
                return 0;
            } else if (position == 1) {
                return 0;
            } else if (position == 2) {
                return 1;
            }
        } else if (getItemCount() == 4) {
            if (mNotes.length == 4) {
                return 0;
            } else {
                if (position == 0) {
                    return 0;
                } else if (position == 1) {
                    return 0;
                } else if (position == 2) {
                    return 0;
                } else if (position == 3) {
                    return 1;
                }
            }
        }
        return 0;
    }

    @Override
    public void onClickNote(NoteViewHolder viewHolder) {
        mOnClickNoteListener.onClickNote(viewHolder.getLayoutPosition());
    }

    @Override
    public void onLongClickNote(NoteViewHolder viewHolder) {
        mOnClickNoteListener.onLongClickNote(viewHolder.getLayoutPosition());
    }

    @Override
    public void onClickAdd(AddViewHolder holder) {
        Intent intent = new Intent(mContext, SelectActivity.class);
        mContext.startActivity(intent);
        ((Activity) mContext).finish();
    }

    public void setOnClickNoteListener(OnClickNoteListener onClickNoteListener) {
        mOnClickNoteListener = onClickNoteListener;
    }

    public void setNotes(UINote[] notes) {
        mNotes = notes;
    }

    public void setNoteOpen(int position) {
        for (UINote mNote : mNotes) {
            mNote.setOpen(false);
        }
        mNotes[position].setOpen(true);
    }

    private void onBindNoteViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((NoteViewHolder) holder).setNoteHead(mNotes[position].getImageUrl());
        ((NoteViewHolder) holder).setNoteName(mNotes[position].getName());
        if (mNotes[position].isOpen()) {
            ((NoteViewHolder) holder).open();
        } else {
            ((NoteViewHolder) holder).close();
        }
    }

}
